package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.R;

public class CreateRoomDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvCreate, tvScriptName;
    private RadioGroup rgReverse, rgPermision;
    private RadioButton rbYes, rbNo, rbAllEnter, rbInviteEnter;
    private LinearLayout llChooseScript;
    private int reverse = 1;
    private int auth = 0;
    private String scriptName, scriptId;

    public CreateRoomDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_create_room, (ViewGroup) null);
        setContentView(dialogView);
        tvCreate = dialogView.findViewById(R.id.tvCreate);
        tvScriptName = dialogView.findViewById(R.id.tvScriptName);
        rgReverse = dialogView.findViewById(R.id.rgReverse);
        rgPermision = dialogView.findViewById(R.id.rgPermision);
        rbYes = dialogView.findViewById(R.id.rbYes);
        rbNo = dialogView.findViewById(R.id.rbNo);
        rbAllEnter = dialogView.findViewById(R.id.rbAllEnter);
        rbInviteEnter = dialogView.findViewById(R.id.rbInviteEnter);
        llChooseScript = dialogView.findViewById(R.id.llChooseScript);

        rgReverse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                if (checkId == R.id.rbYes) {
                    reverse = 1;
                } else if (checkId == R.id.rbNo) {
                    reverse = 0;
                }
            }
        });

        rgPermision.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                if (checkId == R.id.rbAllEnter) {
                    auth = 0;
                } else if (checkId == R.id.rbInviteEnter) {
                    auth = 1;
                }
            }
        });

        llChooseScript.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onChooseScript();
            }
        });

        tvCreate.setOnClickListener(v -> {
            if (null != mCallback) {
                if (!TextUtils.isEmpty(tvScriptName.getText())) {
                    mCallback.onCreate(reverse, auth, scriptId);
                } else {
                    Tooast.normalInfo("请先选择剧本");
                }
            }
        });
    }

    public void setData(String scriptName, String scriptId) {
        this.scriptName = scriptName;
        this.scriptId = scriptId;
        tvScriptName.setText(scriptName);
    }

    public interface Callback {
        void onCreate(int reverse, int auth, String scriptId);

        void onChooseScript();
    }
}
